#include "messageitem.h"
#include "model/datacenter.h"
#include "messageimagelabel.h"

MessageItem::MessageItem(bool isLeft) :isleft(isLeft){}

MessageItem* MessageItem::makeMessageItem(bool isLeft, const model::Message &message)
{
    //构造MessageItem对象
    MessageItem* item = new MessageItem(isLeft);
    //构造布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(0);

    //设置MessageItem对象最小高度
    item->setMinimumHeight(100);
    item->setLayout(layout);

    //创建头像
    QPushButton* avaterBtn = new QPushButton();
    avaterBtn->setFixedSize(QSize(35,35));
    avaterBtn->setIconSize(QSize(35,35));
    avaterBtn->setIcon(message.sender.userIcon);
    avaterBtn->setCursor(Qt::PointingHandCursor);
    avaterBtn->setStyleSheet("QPushButton{border:none;}");

    //创建消息名称,时间
    QLabel* nameAndTimeLabel = new QLabel();
    nameAndTimeLabel->setFixedHeight(20);
    nameAndTimeLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameAndTimeLabel->setText(message.sender.userName + QString(" | ") + message.sendTime);
    nameAndTimeLabel->setStyleSheet("QLabel { font-size: 12px; color: rgb(178, 178,178); }");


    //创建消息体
    QWidget* content = nullptr;

    switch (message.messageType)
    {
    case model::MessageType::TEXT_TYPE:
        content = makeTextMessageItem(message.messageContent,isLeft);
    break;
    case model::MessageType::IMAGE_TYPE:
        content = makeImageMessageItem(message.fileId,message.messageContent,isLeft);
    break;
    case model::MessageType::FILE_TYPE:
        content = makeFileMessageItem(message.fileId,message.messageContent,isLeft,message.fileName);
    break;
    case model::MessageType::SPEECH_TYPE:
        content = makeSoundMessageItem(message.fileId,message.messageContent,isLeft);
    break;
    default:
        LOG() << "创建消息体失败,原因: 未知消息类型";
    break;
    }

    //添加控件到layout中
    if(isLeft)
    {
        layout->addWidget(avaterBtn,0,0,2,1,Qt::AlignLeft | Qt::AlignTop);
        layout->addWidget(nameAndTimeLabel,0,1,Qt::AlignLeft | Qt::AlignBottom);
        layout->addWidget(content,1,1);
    }
    else
    {
        layout->addWidget(avaterBtn,0,1,2,1,Qt::AlignLeft | Qt::AlignTop);
        layout->addWidget(nameAndTimeLabel,0,0,Qt::AlignRight | Qt::AlignBottom);
        layout->addWidget(content,1,0);
    }


    //////////////////////////////////////////////
    /// 点击头像, 弹出用户信息
    /////////////////////////////////////////////
    connect(avaterBtn,&QPushButton::clicked,item,[=](){
        UserInfoWidget* dialog = new UserInfoWidget(message,item);
        dialog->exec();
    });

    ///////////////////////////////////////////////
    /// 个人信息修改时, 消息昵称同步修改
    ///////////////////////////////////////////////
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::changeNicknameDone,item,[=](){
        if(!isLeft)
        {
            nameAndTimeLabel->setText(dataCenter->getMyself()->userName + QString(" | ") + message.sendTime);
        }
    });

    //////////////////////////////////////////////////////////////////
    /// 个人信息修改时,头像同步更改
    //////////////////////////////////////////////////////////////////
    connect(dataCenter, &model::DataCenter::changeAvatarDone,item, [=](){
        avaterBtn->setIcon(dataCenter->getMyself()->userIcon);
    });

    return item;
}



QWidget* MessageItem::makeTextMessageItem(const QByteArray &content, bool isLeft)
{
    MessageContentLabel* item = new MessageContentLabel(model::MessageType::TEXT_TYPE,QString(content),"",content,isLeft);
    return item;
}

QWidget* MessageItem::makeImageMessageItem(const QString &fileId, const QByteArray &content, bool isLeft)
{
    MessageImageLabel* item = new MessageImageLabel(fileId,content,isLeft);
    return item;
}

QWidget* MessageItem::makeFileMessageItem(const QString &fileId, const QByteArray &content, bool isLeft, const QString &fileName)
{
    MessageContentLabel* item = new MessageContentLabel(model::MessageType::FILE_TYPE,"[文件] : " + fileName, fileId, content,isLeft);
    return item;
}

QWidget* MessageItem::makeSoundMessageItem(const QString &fileId, const QByteArray &content, bool isLeft)
{
    MessageContentLabel* item = new MessageContentLabel(model::MessageType::SPEECH_TYPE,"[语音]: ", fileId, content,isLeft);
    return item;
}




